<?php

namespace humhub\modules\api\controllers;

use Yii;
use humhub\components\Controller;
use humhub\modules\content\models\Content;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;

/**
 * 删除dashboard和space内容
 */
class ContentController extends Base4Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        $behaviors['acl'] = [
            'class' => \humhub\components\behaviors\AccessControl::className(),
        ];

        return $behaviors;
    }

    /**
     * 删除dashboard和space内容
     *
     * POST /api/content/delete
     *
     * post.id             要删除内容的ID
     *
     * post.model          固定值[=post]
     *
     * @return array pk:内容对应ID
     */
    public function actionDelete(){
        if( Yii::$app->request->post('model') == 'post' ){
            $_GET['model'] = 'humhub\modules\post\models\Post';
        }else{
            Yii::$app->params['res-success'] = false;
            return false;
        }
        Yii::$app->response->format = 'json';

        $this->forcePostRequest();
        $json = [
            'success' => 'false'
        ];

        $model = Yii::$app->request->get('model');
        $id = (int) Yii::$app->request->post('id');

        $contentObj = Content::get($model, $id);

        if ($contentObj !== null && $contentObj->content->canEdit()) {
            if( $contentObj->created_by==Yii::$app->user->id && $contentObj->delete() ){
                return true;
            }
            /*
            $json = [
                'success' => true,
                //'uniqueId' => $contentObj->getUniqueId(),
                //'model' => $model,
                'pk' => $id
            ];
            return $json;
             */
        }
        Yii::$app->params['res-success'] = false;
        return false;
    }
}
?>
